package cn.acewill.yxd.yunxundian.utils.baiduUtils;

import android.content.Context;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

import org.xutils.ex.DbException;


public class BaiduMapUtils {

	private static BaiduMapUtils instance = null;

	private Context context;

	public static BaiduMapUtils getInstance(Context context) {
		if (instance == null) {
			instance = new BaiduMapUtils();
		}
		instance.context = context;
		return instance;
	}

	private BaiduMapUtils() {
	}

	// 定位相关
	private LocationClient mLocClient;

	/**
	 * 通过定位获取当前坐标
	 * @param onLocationListener
	 */
	public void getLocate(final OnLocationMapListener onLocationListener) {
		// 定位耗时操作，给出提示框

		// 定位初始化
		mLocClient = new LocationClient(context);
		mLocClient.registerLocationListener(new BDLocationListener() {
			@Override
			public void onReceiveLocation(BDLocation location) {

				if (location == null) {

					return;
				}
				try {
					onLocationListener.getCurrLocation(location);
				} catch (DbException e) {
					e.printStackTrace();
				}

			}

			@Override
			public void onConnectHotSpotMessage(String s, int i) {

			}
		});
		LocationClientOption option = new LocationClientOption();
		option.setIsNeedAddress(true);// 是否需要获取定位的详细信息
		option.setOpenGps(true); // 打开gps
		option.setCoorType("bd09ll"); // 设置坐标类型
		// option.setScanSpan(1000);//设置这个参数，stop()将无法关闭 定位
		mLocClient.setLocOption(option);
		mLocClient.start();
	}



	public interface OnLocationMapListener {
		public void getCurrLocation(BDLocation location) throws DbException;
	}
}
